package com.example.sfm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sfm.exception.ObjectException;
import com.example.sfm.mapper.CoursesMapper;
import com.example.sfm.pojo.dto.CourseDto;
import com.example.sfm.pojo.dto.CourseQueryDto;
import com.example.sfm.pojo.entity.Courses;
import com.example.sfm.pojo.vo.CourseVo;
import com.example.sfm.result.PageResult;
import com.example.sfm.service.ICoursesService;
import com.example.sfm.service.ITeachersService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@Service
@RequiredArgsConstructor
public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> implements ICoursesService {
    private final ITeachersService teachersService;

    @Override
    public void createCourse(CourseDto courseDto) {
        Courses course = BeanUtil.copyProperties(courseDto, Courses.class);
        if (!save(course)) {
            throw new ObjectException("创建课程失败！");
        }
    }

    @Override
    public PageResult<?> listCourses(@NotNull CourseQueryDto dto) {
        String courseName = dto.getCourseName();
        String courseCode = dto.getCourseCode();
        String semester = dto.getSemester();
        Integer teacherId = dto.getTeacherId();
        LambdaQueryWrapper<Courses> queryWrapper = new LambdaQueryWrapper<Courses>()
                .like(StringUtils.isNoneEmpty(courseName), Courses::getCourseName, courseName)
                .eq(StringUtils.isNotEmpty(courseCode), Courses::getCourseCode, courseCode)
                .eq(teacherId != null, Courses::getTeacherId, teacherId)
                .eq(StringUtils.isNotEmpty(semester), Courses::getSemester, semester);
        Page<Courses> page = page(new Page<>(dto.getPageNum(), dto.getPageSize()), queryWrapper);
        return new PageResult<>(page.getTotal(), page.getRecords().stream()
                .map(courses -> BeanUtil.copyProperties(courses, CourseVo.class).setTeacherName(teachersService.getById(courses.getTeacherId()).getTeacherName())).toList());
    }

    @Override
    public void updateCourse(Long id, CourseDto courseDto) {
        Courses course = getById(id);
        if (course == null) {
            throw new ObjectException("课程不存在！");
        }
        BeanUtil.copyProperties(courseDto, course);
        if (!updateById(course)) {
            throw new ObjectException("更新课程失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCourses(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ObjectException("课程ID列表不能为空！");
        }
        List<Long> distinctIds = ids.stream().distinct().toList();
        if (!removeBatchByIds(distinctIds)) {
            throw new ObjectException("删除课程失败！");
        }
    }
}
